package leetCode.firstWeek;

import java.util.Stack;

public class Code_32 {
  public int longestValidParentheses(String s) {
    Stack<Character> str = new Stack<>();
    Stack<Integer> left_posi = new Stack<>();
    int[] zero_one = new int[s.length()];
    for (int i = 0; i < s.length(); i++) {
      if (s.charAt(i) == '(') {
        left_posi.push(i);
      }
      if (!str.empty() && str.peek() == '(' && s.charAt(i) == ')') {
        str.pop();
        zero_one[left_posi.pop()] = 1;
        zero_one[i] = 1;
        continue;
      }
      zero_one[i] = 0;
      str.push(s.charAt(i));
    }
    String tmp = "";
    for (int i : zero_one) {
      tmp += i;
    }
    String[] tmpArr = tmp.split("0");
    String max = "";
    for (String i : tmpArr) {
      if (max.length() < i.length()) {
        max = i;
      }
    }
    return max.length();
  }
}
